package com.hornsun.client.personalPage.api;

import com.hornsun.client.personalPage.dto.ExchangeContentDTO;
import com.hornsun.client.personalPage.dto.ExchangeHintDTO;
import com.hornsun.client.personalPage.dto.HealthBeansRecordDTO;
import com.hornsun.client.personalPage.service.HealthBeansService;
import com.hornsun.domain.constant.types.HealthBeansSource;
import com.hornsun.domain.constant.types.UseHealthBeans;
import com.hornsun.domain.dbo.Coupon;
import com.hornsun.domain.dbo.HealthBeansRecord;
import com.hornsun.domain.dbo.MemberCoupon;
import com.hornsun.domain.dbo.MemberWechat;
import com.hornsun.repository.CouponRepository;
import com.hornsun.repository.HealthBeansReposiory;
import com.hornsun.repository.MemberCouponRepository;
import com.hornsun.repository.MemberWechatRepository;
import com.hornsun.util.LittleUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by 编译中... on 2018/4/24.
 */
@RestController
@RequestMapping("/api/healthBeans")
public class HealthBeansAPI {
    @Resource
    private HealthBeansService healthBeansService;

    @Resource
    private CouponRepository couponRepository;

    @Resource
    private MemberCouponRepository memberCouponRepository;

    @Resource
    private MemberWechatRepository memberWechatRepository;

    @Resource
    private HealthBeansReposiory healthBeansReposiory;


    /**
     * 获取健康豆记录
     * @param httpRequest
     * @return healthBeans:健康豆数; list:健康豆收支记录
     */
    @GetMapping("/healthBeansRecord")
    public ResponseEntity<?> healthBeansRecord(HttpServletRequest httpRequest, @RequestParam("page")int page){
        String openid = httpRequest.getAttribute("openid").toString();
        int healthBeans = healthBeansService.queryHealthBeans(openid);
        List<HealthBeansRecord> list = healthBeansService.queryHealthBeansRecord(openid,page);
        HealthBeansRecordDTO healthBeansRecordDTO = new HealthBeansRecordDTO();
        healthBeansRecordDTO.setHealthBeans(healthBeans);
        healthBeansRecordDTO.setList(list);
        healthBeansRecordDTO.setWhetherSignIn(healthBeansReposiory.whetherSignIn(openid));
        return new ResponseEntity<Object>(healthBeansRecordDTO, HttpStatus.OK);
    }


    /**
     * 签到
     * @param httpRequest
     * @return
     */
    @Transactional
    @PostMapping("/signIn")
    public ResponseEntity<?> signIn(HttpServletRequest httpRequest){
        String openid = httpRequest.getAttribute("openid").toString();
        if (healthBeansReposiory.whetherSignIn(openid)){
            int count = LittleUtil.getRandom(6);
            healthBeansService.gainHealthBeans(openid,count, HealthBeansSource.SIGNIN.getName());
            return new ResponseEntity<Object>(count,HttpStatus.OK);
        }else {
            return new ResponseEntity<Object>("不能重复签到！",HttpStatus.OK);
        }

    }


    /**
     * 获取可兑换内容
     * @param httpRequest
     * @return
     */
    @GetMapping("/getExchangeContent")
    public ResponseEntity<?> getExchangeContent(HttpServletRequest httpRequest){
        String openid = httpRequest.getAttribute("openid").toString();
        MemberWechat memberWechat = memberWechatRepository.queryByOpenid(openid);
        ExchangeContentDTO exchangeCouponDTO = new ExchangeContentDTO();
        exchangeCouponDTO.setHealthBeans(memberWechat.getHealthBeans());
        exchangeCouponDTO.setCouponList(couponRepository.getCoupon());
        return new ResponseEntity<Object>(exchangeCouponDTO,HttpStatus.OK);
    }

    /**
     * 兑换优惠券
     * @param httpRequest
     * @param couponGuid
     * @return
     */
    @Transactional
    @PostMapping("/exchangeCoupon")
    public ResponseEntity<?> exchangeCoupon(HttpServletRequest httpRequest,
                                            @RequestParam("guid")String couponGuid){
        String openid = httpRequest.getAttribute("openid").toString();
        int healthbeans = memberWechatRepository.queryByOpenid(openid).getHealthBeans();
        int count = couponRepository.queryByGuid(couponGuid).getExchange();
        if (healthbeans>count){
            healthBeansService.gainHealthBeans(openid,-count, UseHealthBeans.EXCHANGECOUPON.getName());
            Coupon coupon = couponRepository.queryByGuid(couponGuid);
            MemberCoupon memberCoupon = new MemberCoupon();
            Date gainTime= new Date();
            Date expirationTime = new Date(gainTime.getTime()+coupon.getValidity()*24*60*60*1000);
            memberCoupon.setMemberCouponGuid(UUID.randomUUID().toString());
            memberCoupon.setOpenid(openid);
            memberCoupon.setCouponGuid(couponGuid);
            memberCoupon.setGainTime(gainTime);
            memberCoupon.setExpirationTime(expirationTime);
            memberCouponRepository.addMemberCoupon(memberCoupon);
            ExchangeHintDTO exchangeHintDTO = new ExchangeHintDTO();
            exchangeHintDTO.setCoupon(coupon);
            exchangeHintDTO.setTime(gainTime);
            return new ResponseEntity<Object>(exchangeHintDTO,HttpStatus.OK);
        }else {
            return new ResponseEntity<Object>("健康豆不足！",HttpStatus.OK);
        }

    }
}
